#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include "bonc_tool.h"

typedef void EMAIL(const char *url);
typedef struct UandDo
{
    const char *url;
    EMAIL *sendemail;
} UandDo;

void do_after(const char *url)
{
    char buf[1024];
    char path[1024];

    getcwd(path, 1024);
    sprintf(buf, "echo \"恭喜！url=%s的文件下载成功，下载路径为:%s，请注意查收\" | mail -s \"恭喜！文件下载成功\" v13b837@163.com", url, path);
    system(buf);
}

void *threadhandler(void *Ud)
{
    char buf[1024];
    UandDo *tem = (UandDo *)Ud;

    sprintf(buf, "curl  %s  -O", tem->url);
    system(buf);
    tem->sendemail(tem->url);
}

void BONC_NB_download(const char *url, EMAIL *sendemail)
{
    UandDo Ud;
    pthread_t thfd;
    Ud.url = url;
    Ud.sendemail = sendemail;

    if (pthread_create(&thfd, NULL, threadhandler, (void *)&Ud) != 0)
    {
        printf("false!\r\n");
    }

    sleep(1);
    return;
}

int main(int argc, char *argv[])
{
    if (2 > argc)
    {
        bonc_printf_In("Please enter url!  Suchas : mydownload https://7-zip.org/a/7z2201-x64.exe");
        return -1;
    }
    BONC_NB_download(argv[1], do_after);

    return 0;
}
